<?
class page_admin
{
	var $template="list";
	
	var $modes = array("fck"=>"Визуальный редактор","html"=>"HTML код");
	
	function Render()
	{
		$form = new Form();
		$form->table="pages";
		$form->hasPos=false;
		$form->doAdd=false;
		$form->doEdit=false;
		$form->doUpdate=false;
		
		if($_REQUEST['id'])
		{
			$block = DBSingleRow( "select * from pages where id=".intval($_REQUEST['id']) );
			$form->data_source = $block;
			$form->data_source['fck']  = $form->data_source['content'];
			$form->data_source['html'] = $form->data_source['content'];
		}
		if($_REQUEST["mode"]=='fck')  $_REQUEST["tex"] = $_REQUEST["fck"];
		if($_REQUEST["mode"]=='html') $_REQUEST["tex"] = $_REQUEST["html"];
		
		$form->Add(new           Str("title",   "Заголовок"          ));
		$form->Add(new           Str("url",     "URL"      ));
		$form->Add(new           Str("keywords",     "Ключевые слова"      ));
		$form->Add(new           Str("description",     "Описание"      ));
		$form->Add(new        Select("mode",    "Режим редактирования",    $this->modes  ));
		//$form->Add(new           FCK("fck",     "Текст"              ));
		$form->Add(new      Textarea("html",    "Шаблон"             ));
		$form->Add(new ValidatorNotEmpty("title",   'Введите заголовок'       ));
		$form->Add(new ValidatorNotEmpty("url",     'Введите URL'   ));
		
		$bt = new FormCondition("mode");
		$bt->CreateRule(Array("","fck"),    array("fck"),    array("html") );
		$bt->CreateRule("html",             array("html"),   array("fck")  );
		$form->AddCondition($bt);
		
		if($_REQUEST["formaction"]=='add' || $_REQUEST["formaction"]=='update')
		{
			$title = mysql_real_escape_string($_REQUEST["title"]);
			$url   = mysql_real_escape_string($_REQUEST["url"]);
			$tex   = mysql_real_escape_string($_REQUEST["tex"]);
			$mode  = mysql_real_escape_string($_REQUEST["mode"]);
			$keywords = mysql_real_escape_string($_REQUEST["keywords"]);
			$description = mysql_real_escape_string($_REQUEST["description"]);
		}
		if($_REQUEST["formaction"]=='add' && $form->Validate() )
		{
			DBInsert($form->table,Array('title','url','content','mode','keywords','description'), Array('title'=>$title,'url'=>$url,'content'=>$tex,'mode'=>$mode,'keywords'=>$keywords,'description'=>$description));
			ClearGet('pages.php');
		}
		if($_REQUEST["formaction"]=='update' && $form->Validate() )
		{
			$id = intval($_REQUEST['id']);
			DBUpdate($form->table,Array('title','url','content','mode','keywords','description'), Array('title'=>$title,'url'=>$url,'content'=>$tex,'mode'=>$mode,'keywords'=>$keywords,'description'=>$description,'id'=>$id));
			ClearGet('pages.php');
		}
		
		$html_form = $form->Render();
		
		
		$dao = Array();
		$pagerSelect = new Pager("select id,title from pages" );
		$pagerSelect->def_sortby="id";
		$pagerSelect->hasPos=false;
		$dao["rows"] = $pagerSelect->GetRows();
		$dao["pager"] = $pagerSelect->RenderPager();
		$dao["listtitle"] = GetAdminTitle();
		$dao["haspos"]=false;
		
		
		$html_list = TemplaterParse(GetAdminTemplate($this->template),$dao);
		
		echo RenderAdmin($html_form,$html_list);
	}
}
